package cn.dduan.fool.design.pattern.behavior.mediator;


/**
 * 中介者模式
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
public class MediatorPatternOne {
    public static void main(String[] args) {
        // 一个房主、一个租房者、一个中介机构
        MediatorStructure mediator = new MediatorStructure();

        //房主和租房者只需要知道中介机构即可
        HouseOwner houseOwner = new HouseOwner("张三", mediator);
        Tenant tenant = new Tenant("李四", mediator);

        //中介结构要知道房主和租房者
        mediator.setHouseOwner(houseOwner);
        mediator.setTenant(tenant);

        tenant.constact("听说你那里有三室的房主出租.....");
        houseOwner.constact("是的!请问你需要租吗?");
        tenant.constact("是的！我要租房.....");
        houseOwner.constact("月租5000元");
    }
}

/**
 * 抽象中介者
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
abstract class MediatorOne {
    /**
     * 申明一个联络方法
     *
     * @param message
     * @param person
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void constact(String message, Person person);
}

/**
 * 具体中介者
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class MediatorStructure extends MediatorOne {
    /**
     * 首先中介结构必须知道所有房主和租房者的信息
     */
    private HouseOwner houseOwner;
    private Tenant tenant;

    public HouseOwner getHouseOwner() {
        return houseOwner;
    }

    public void setHouseOwner(HouseOwner houseOwner) {
        this.houseOwner = houseOwner;
    }

    public Tenant getTenant() {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    @Override
    public void constact(String message, Person person) {
        if (person == houseOwner) {
            //如果是房主，则租房者获得信息
            tenant.getMessage(message);
        } else {
            //反之则是获得房主信息
            houseOwner.getMessage(message);
        }
    }
}

/**
 * 抽象同事类
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
abstract class Person {
    protected String name;
    protected MediatorOne mediator;

    public Person(String name, MediatorOne mediator) {
        this.name = name;
        this.mediator = mediator;
    }

    /**
     * 与中介者联系
     *
     * @param message
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void constact(String message);

    /**
     * 获取信息
     *
     * @param message
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void getMessage(String message);
}

/**
 * 具体同事类：租客
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class HouseOwner extends Person {

    HouseOwner(String name, MediatorOne mediator) {
        super(name, mediator);
    }

    /**
     * @param message
     * @return void
     * @desc 与中介者联系
     */
    @Override
    public void constact(String message) {
        mediator.constact(message, this);
    }

    /**
     * @param message
     * @return void
     * @desc 获取信息
     */
    @Override
    public void getMessage(String message) {
        System.out.println("租客:" + name + ",获得信息：" + message);
    }
}

/**
 * 具体同事类：房东
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class Tenant extends Person {

    Tenant(String name, MediatorOne mediator) {
        super(name, mediator);
    }

    /**
     * @param message
     * @return void
     * @desc 与中介者联系
     */
    @Override
    public void constact(String message) {
        mediator.constact(message, this);
    }

    /**
     * @param message
     * @return void
     * @desc 获取信息
     */
    @Override
    public void getMessage(String message) {
        System.out.println("房东:" + name + ",获得信息：" + message);
    }
}
